package com.yvon.maple.service.msg.core.websocket.processor;

import cn.hutool.json.JSONUtil;
import com.yvon.maple.service.msg.core.websocket.message.Message;
import io.netty.channel.Channel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 处理器
 *
 * @author : Yvon
 * @since : 2021-12-02
 */
public abstract class Processor<E extends Message> {

    public abstract void exec(Channel channel, String msg);


    public E parse(String msg){
      return JSONUtil.toBean(msg, getMessageClass());
    }

    private Class<E> getMessageClass() {
        ParameterizedType parameterizedType = (ParameterizedType)this.getClass().getGenericSuperclass();
        Type entityType = parameterizedType.getActualTypeArguments()[0];
        return (Class)entityType;
    }
}
